package b1w1nn1ng.crossf1t.business;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class ReportGenerator extends TimerTask {

  public void run() {
    System.out.println("Generating report");
    
    String to = "sirusto@gmail.com";
    String from = "b1w@crossfit.com";
    String host = "smtp.yourisp.net"; //?

    // Create properties, get Session
    Properties props = new Properties();

    // If using static Transport.send(),
    // need to specify which host to send it to
    props.put("mail.smtp.host", host);
    
    props.put("mail.debug", "true");
    
    Session session = Session.getInstance(props);

    try {
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject("CrossFit Report");
        msg.setSentDate(new Date());
        msg.setText("This is a test email.");

        Transport.send(msg);
    }
    catch (MessagingException e) {
        e.printStackTrace();
    }
  }
}

class Scheduler {

  public static void main(String[] args) {
	Timer timer = new Timer();
	Calendar date = Calendar.getInstance();
	date.set(
	  Calendar.DAY_OF_WEEK,
	  Calendar.SUNDAY
	);
	date.set(Calendar.HOUR, 0);
	date.set(Calendar.MINUTE, 0);
	date.set(Calendar.SECOND, 0);
	date.set(Calendar.MILLISECOND, 0);
	// Schedule to run every Sunday in midnight
	timer.schedule(new ReportGenerator(), date.getTime(), 1000 * 60 * 60 * 24 * 7);
  }
}